package com.qqtech.qquan.draw.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.coupon.model.CouponBaseQuery;
import com.qqtech.qquan.coupon.model.CouponBaseVo;
import com.qqtech.qquan.coupon.service.CouponBaseService;
import com.qqtech.qquan.draw.model.DrawItem;
import com.qqtech.qquan.draw.model.DrawItemQuery;
import com.qqtech.qquan.draw.model.DrawItemVo;
import com.qqtech.qquan.draw.service.DrawBaseService;
import com.qqtech.qquan.draw.service.DrawItemService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/draw/item")
public class DrawItemController extends BaseAdminController {
	@Resource
	private DrawItemService drawItemService;

	@Resource
	private DrawBaseService drawBaseService;
	
	@Resource
	private CouponBaseService couponBaseService;
	
	@Override
	protected BaseService<DrawItem> getBaseService() {
		return drawItemService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/draw/drawItem-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(DrawItemQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), new PageOrder(Direction.DESC, "id"));
		Page<DrawItemVo> page = drawItemService.queryPageListAdmin(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/imgUpload", method = RequestMethod.GET)
	public String imgUpload(Model model) {
		return "/draw/drawItem-imgUpload";
	}
	
	@RequestMapping(value = "/detail")
	public String detail(Integer id, Model model) {
		DrawItemVo detailVo = drawItemService.getById(id);
		String category="";
		model.addAttribute("category", category);
		model.addAttribute("detailVo", detailVo);
		return "/draw/drawItem-detail";
	}
	
	/**
	 * 创建成功
	 * 
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(DrawItem addItem) {
		Result ret = new Result();
		drawItemService.insert(addItem);
		return ret;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		CouponBaseQuery couponBaseQuery=new CouponBaseQuery();
		couponBaseQuery.setStatus(YesNoEnum.YES.getCode());
		List<CouponBaseVo> couponList=couponBaseService.queryList(couponBaseQuery);
		model.addAttribute("couponList", couponList);
		return "/draw/drawItem-add";
	}
	
	/**
	 * 去更新
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/toUpdate")
	public String toUpdate(Integer id, Model model) {
		DrawItemVo drawItemVo = drawItemService.getById(id);
		model.addAttribute("drawItemVo", drawItemVo);
		DrawItemQuery drawItemQuery = new DrawItemQuery();
		CouponBaseQuery couponBaseQuery=new CouponBaseQuery();
		couponBaseQuery.setStatus(YesNoEnum.YES.getCode());
		List<CouponBaseVo> couponList=couponBaseService.queryList(couponBaseQuery);
		List<DrawItemVo> list = drawItemService.queryList(drawItemQuery);
		model.addAttribute("list", list);
		model.addAttribute("couponList", couponList);
		return "/draw/drawItem-update";
	}
	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(DrawItem drawItem) { 
		Result ret = new Result();
		drawItemService.updateById(drawItem	);
		return ret;
	}
	
	 

}
